//
//  TMTabbarPlayView.swift
//  TimMusic
//
//  Created by nkl on 2024/10/15.
//

import Foundation

class TMTabbarPlayView: UIControl {
    lazy var iconButton: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "ic_tab_icon"), for: .normal)
        return btn
    }()

    lazy var iconImage: UIImageView = .simpleImage(imageName: "ic_small_placeholder")

    lazy var icBgView: UIImageView = .simpleImage(imageName: "ic_tab_playbg")

    lazy var timeLabel: UILabel = .simpleLabel(text: "00:00", font: .systemFont14, color: .white)

    lazy var pauseButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "ic_tab_play"), for: .normal)
        btn.setImage(UIImage(named: "ic_tab_pause"), for: .selected)
        return btn
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        addChildren()
        makeConstraints()
    }

    func addChildren() {
        addSubview(icBgView)
        addSubview(iconButton)
        iconImage.layer.cornerRadius = 16
        iconImage.layer.masksToBounds = true
        iconButton.addSubview(iconImage)
        addSubview(timeLabel)
        addSubview(pauseButton)
    }

    func makeConstraints() {
        iconButton.snp.makeConstraints { make in
            make.leading.top.bottom.equalToSuperview()
            make.width.height.equalTo(52)
        }

        iconImage.snp.makeConstraints { make in
            make.width.height.equalTo(32)
            make.center.equalToSuperview()
        }

        icBgView.snp.makeConstraints { make in
            make.leading.equalTo(iconButton.snp.centerX)
            make.height.equalTo(iconButton)
            make.centerY.equalTo(iconButton)
            make.trailing.equalToSuperview()
        }

        timeLabel.snp.makeConstraints { make in
            make.leading.equalTo(iconButton.snp.trailing).offset(8)
            make.trailing.equalTo(pauseButton.snp.leading).offset(-8)
            make.centerY.equalToSuperview()
        }

        pauseButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.width.height.equalTo(24)
            make.centerY.equalToSuperview()
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
